package jml.defaultimple.util;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class UDate
{
	public static String time()
	{
		GregorianCalendar g = new GregorianCalendar();
		g.setTimeInMillis(System.currentTimeMillis());
		
		int iH = g.get(Calendar.HOUR)+(g.get(Calendar.AM_PM)==Calendar.AM?0:12);
		String sH = UString.lpad(Integer.toString(iH), '0', 2);
		String sM = UString.lpad(Integer.toString(g.get(Calendar.MINUTE)), '0', 2);
		String sS = UString.lpad(Integer.toString(g.get(Calendar.SECOND)), '0', 2);
		
		return sH+":"+sM+":"+sS;
	}
	
	public static String nowTime()
	{
		return now()+" "+time();
	}
	

	
	public static String addDays(String f, int n)
	{
		GregorianCalendar g = new GregorianCalendar();
		g.setTimeInMillis(stringToLong(f));
		g.add(Calendar.DAY_OF_MONTH, n);
		return longToString(g.getTimeInMillis());
	}
	
	public static String longToString(long f)
	{
		GregorianCalendar g = new GregorianCalendar();
		g.setTimeInMillis(f);
		String y = UString.lpad(Integer.toString(g.get(Calendar.YEAR)),'0',4);
		String m = UString.lpad(Integer.toString(g.get(Calendar.MONTH)+1),'0',2);
		String d = UString.lpad(Integer.toString(g.get(Calendar.DAY_OF_MONTH)),'0',2);
		
		return y+"/"+m+"/"+d;
	}
	
	public static long stringToLong(String f)
	{
		int p1 = f.indexOf("/");
		int p2 = f.lastIndexOf("/");
		
		int y = Integer.parseInt(f.substring(0,p1));
		int m = Integer.parseInt(f.substring(p1+1,p2));
		int d = Integer.parseInt(f.substring(p2+1));
		
		GregorianCalendar g = new GregorianCalendar(y,m-1,d);
		
		return g.getTimeInMillis();
	}
	
	public String toString(int y, int m, int d)
	{
		GregorianCalendar g = new GregorianCalendar(y,m-1,d);
		return longToString(g.getTimeInMillis());
	}
	
	public static String now()
	{
		return longToString(System.currentTimeMillis());
	}
	
	public static int daysBetween(String f1, String f2)
	{
		// obtengo la fecha mas antigua
		String fMenor = f1.compareTo(f2)<0?f1:f2;
		
		// obtengo la fecha posterior
		String fMayor = f1.compareTo(f2)<0?f2:f1;
		
		long l1 = stringToLong(f1);
		long l2 = stringToLong(f2);

		long dif = Math.abs(l1-l2);
		
		if( dif == 0 )
		{
			return 0;
		}
		
		int dias = 1;
		fMenor = addDays(fMenor,1);
		while( fMenor.compareTo(fMayor)<0 )
		{
			dias++;
			fMenor = addDays(fMenor, 1);
		}
		
		return dias;
	}
	
	public static void main(String[] args)
	{	
		System.out.println(time());
	}
}
